import {
  Controller,
  Get,
  Inject,
  Logger,
  Post,
  Req,
  Session,
  UseGuards
} from '@nestjs/common';
import { ManagerService } from './manager.service';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import { LocalGuard, Public } from '@/common/index';
import { AuthService } from '@/business/auth/auth.service';
import { LoginDto } from '@/business/auth/dto/login.dto';
// 管理后台
@ApiTags('管理后台-全局')
@Controller()
export class ManagerController {
  constructor(
    private readonly managerService: ManagerService,
    private readonly authService: AuthService
  ) {}

  // 获取验证码
  @ApiOperation({ summary: '获取验证码' })
  @Public() // 定义为公开接口，绕过jwt守卫
  @Get('captcha')
  getCaptcha(@Session() session: any) {
    session.codeId = session?.id;
    return this.managerService.createCaptch(session.codeId);
  }

  // 登录
  @ApiOperation({ summary: '登录' })
  @ApiBody({
    description: '登录',
    type: LoginDto,
    required: true
  })
  @Public() // 定义为公开接口，绕过jwt守卫
  @UseGuards(LocalGuard) // 使用本地策略
  @Post('login')
  login(@Req() req: any) {
    return this.authService.login(req.user);
  }

  @ApiOperation({ summary: '退出登录' })
  @ApiBearerAuth()
  @Post('logout')
  logout(@Req() req) {
    const token = req.header('authorization').split(' ')[1];
    return this.authService.logout(token);
  }

  // 首页面板
  @ApiOperation({ summary: '首页面板' })
  @ApiBearerAuth() // swagger备注 需要token
  @Get('dashboard')
  dashboard() {
    return this.managerService.getDashboard();
  }
}
